package com.stars.home.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import androidx.lifecycle.Observer
import com.hwangjr.rxbus.RxBus
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.R
import com.stars.home.adapter.MsgAdapter
import com.stars.home.base.MBRActivity
import com.stars.home.config.ConfigRx
import com.stars.home.databinding.ActivityMsgBinding
import com.stars.home.model.TrMsgList
import com.stars.home.vmodel.MsgVModel
import com.trailer.base.net.IResp

class MsgActivity : MBRActivity<MsgVModel, MsgAdapter, ActivityMsgBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        titleBar()?.tvRight?.text="全部已读"
        titleBar()?.tvRight?.visibility=View.VISIBLE

        vm.ll=binding.ll

        vm.index.observe(this){
            adapter().removeAt(it)
        }


    }



    override fun vmClass(): Class<MsgVModel> {
        return MsgVModel::class.java
    }
    override fun binding(): ActivityMsgBinding {
        return ActivityMsgBinding.inflate(layoutInflater)
    }

    override fun initAdapter(): MsgAdapter {
        return MsgAdapter(vm)
    }


    override fun title(): String? {
        return getString(R.string.msg)
    }

    override val isRxbus: Boolean
        get() = true

    override fun listeners() {
        super.listeners()

        titleBar()?.tvRight?.onClick {
            //设为全部已读
            vm.toSetRead()
        }
    }




    @Subscribe(tags = [Tag(ConfigRx.MSG_RED)])
    fun setRefreshMsg(position: String) {
        if(!TextUtils.isEmpty(position)){

            //未读消息变成已读消息
            var index=position.toInt()
            var item=adapter().getItem(index)
             item.isRead=1
            adapter().notifyItemChanged(index)

        }else{
            onRefresh()
        }

    }

}
